﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using System.Windows.Forms;
using System.Data;
using System.Data.SQLite;

namespace FileTaggerData.Helpers
{
    public class FactoryHolder
    {
        private static ISessionFactory _factory;

        public static ISession GetSession(bool _conn = false)
        {
            //MessageBox.Show(AppDomain.CurrentDomain.BaseDirectory);
            if (_factory == null)
            {
                NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
                Dictionary<string, string> props = new Dictionary<string, string>();
                //cfg.Configure(string.Format("{0}\\SQLite.cfg.xml",AppDomain.CurrentDomain.BaseDirectory));
                props.Add("connection.driver_class","NHibernate.Driver.SQLite20Driver");
		        props.Add("connection.connection_string",string.Format(@"Data Source={0}\DataBase\FileTags.db;Version=3",AppDomain.CurrentDomain.BaseDirectory));
		    	props.Add("dialect","NHibernate.Dialect.SQLiteDialect");
		        props.Add("query.substitutions","true=1;false=0");
		        props.Add("proxyfactory.factory_class","NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu");
                props.Add("show_sql", "true");
                cfg.AddProperties(props);
                cfg.AddAssembly(typeof(FileTaggerData.Domain.FileTag).Assembly);

                _factory = cfg.BuildSessionFactory(); ;
            }

            return _factory.OpenSession();
        }
    }
}
